package com.xh.test;

import java.util.*;

import com.xh.bussiness.tiku.constant.TikuEsContant;
import com.xh.core.annotation.ExcludeLoginTokenIntercept;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xh.bussiness.tiku.model.Question;
import com.xh.core.bean.AjaxResult;
import com.xh.core.bean.XhPage;
import com.xh.core.service.IElasticSearchService;

@RestController
@RequestMapping("/es")
public class EsTestController {

	@Autowired
	private IElasticSearchService esService;

	private String index = "dev_book_chapter_tree_index";

	private String type = "dev_book_chapter_tree_type_english3";

	@PostMapping("/add")
	public AjaxResult add(Question quest) {
		return new AjaxResult(true,esService.addIndex(quest, quest.getId(), index, type));
	}
	
	@PostMapping("/insertOrUpdate")
	public AjaxResult insertOrUpdate() {
		return new AjaxResult(true);
	}

	@ExcludeLoginTokenIntercept
	@PostMapping("/queryAll")
	public AjaxResult queryAll(String param) {
		return new AjaxResult(true,esService.queryAll(index, type, param, Question.class));
	}
	
	@PostMapping("/queryByPageMap")
	public AjaxResult queryByPage(XhPage<Question> page, String key, String value,  String key1, String value1) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(key, value);
		map.put(key1, value1);
		return new AjaxResult(true,esService.queryByPage(map, index, type, page, Question.class));
	}
	
	@PostMapping("/queryByPage")
	public AjaxResult queryByPage(XhPage<Question> page, String name, String text) {
		QueryBuilder query = QueryBuilders.matchQuery(name, text);
		return new AjaxResult(true,esService.queryByPage(query, index, type,  page, Question.class));
	}

	@ExcludeLoginTokenIntercept
	@PostMapping("/matchQuery")
	public AjaxResult matchQuery(String name, String text) {
		return new AjaxResult(true,esService.matchQuery(name, text, index, type, Map.class));
	}

	@PostMapping("/matchQueryMap")
	public AjaxResult matchQueryMap(String name, String text) {
		return new AjaxResult(true,esService.matchQuery(name, text, index, type, Map.class));
	}
	
	@PostMapping("/matchQueryPercent")
	public AjaxResult matchQueryPercent(String name, String text, String percent) {
		return new AjaxResult(true,esService.matchQueryPercent(name, text, percent, index, type));
	}

	@PostMapping("/matchQueryPercent2")
	public AjaxResult matchQueryPercent2(String name, String text, String percent) {
		Map map = esService.matchQueryPercent(name, text, percent, TikuEsContant.ES_INDEX_TIKU,
				TikuEsContant.TYPE.ES_INDEX_TYPE_QUESTION);

		Object hits = map.get("hits");
		Object hits1 = ((Map) hits).get("hits");
		List<Map> hits11 = (List<Map>) hits1;
		if(hits11.size() > 0){
			System.out.println("1111111111111");
		}

		return new AjaxResult(true, hits);
	}

	@PostMapping("/fuzzyQuery2")
	public AjaxResult fuzzyQuery2(String name, String text) {
		return new AjaxResult(true,esService.fuzzyQuery(name, text, TikuEsContant.ES_INDEX_TIKU,
				TikuEsContant.TYPE.ES_INDEX_TYPE_QUESTION, Question.class));
	}
	
	@PostMapping("/fuzzyQuery")
	public AjaxResult fuzzyQuery(String name, String text) {
		return new AjaxResult(true,esService.fuzzyQuery(name, text, index, type, Question.class));
	}
	@ExcludeLoginTokenIntercept
	@PostMapping("/termQuery")
	public AjaxResult termQuery(String name, String text) {
		return new AjaxResult(true,esService.termQuery(name, text, index, type, Map.class));
	}
	
	@PostMapping("/termsQuery")
	public AjaxResult termsQuery(String name, @RequestParam(name="text")String[] text) {
		return new AjaxResult(true,esService.termsQuery(name, Arrays.asList(text), index, type, Question.class));
	}
}
